package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.building.*;
import com.yanqu.road.utils.property.PropertyHelper;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CollectionDaoImpl extends TempDao {

	public Map<Integer, CollectionInfo> getCollectionInfoMap() {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_collection";
		Map<Integer, CollectionInfo> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				CollectionInfo data = new CollectionInfo();
				data.setId(rs.getInt("id"));
				data.setUnlockType(rs.getInt("unlockType"));
				String unlockParam = rs.getString("unlockPar");
				String[] arrStr = unlockParam.split("=");
				if(arrStr.length > 1){
					data.setNeedBuildingId(Integer.parseInt(arrStr[0]));
					data.setNeedBuildingLv(Integer.parseInt(arrStr[1]));
				}
				dataMap.put(data.getId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public Map<Integer, CollectionUpgrade> getCollectionUpgradeMap() {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_collection_upgrade";
		Map<Integer, CollectionUpgrade> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				CollectionUpgrade data = new CollectionUpgrade();
				data.setLevel(rs.getInt("id"));
				data.setEarnSpeedAddition(rs.getInt("earnSpeedAddition"));
				data.setProbability(rs.getInt("probability"));
				data.setConsume(PropertyHelper.parseStringToProperty(rs.getString("consume")));
				dataMap.put(data.getLevel(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public Map<Integer, CollectionPavilion> getCollectionPavilionMap() {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_collection_pavilion";
		Map<Integer, CollectionPavilion> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				CollectionPavilion data = new CollectionPavilion();
				data.setLevel(rs.getInt("level"));
				data.setNeedCollectionTotalLv(rs.getInt("limit"));
				data.setConsume(PropertyHelper.parseStringToProperty(rs.getString("consume")));
				data.setAddPatronsQualification(rs.getInt("addQualification"));
				dataMap.put(data.getLevel(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}

		return dataMap;
	}
}